package com.example.myrecycle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class StarAdapter extends RecyclerView.Adapter<StarAdapter.ViewHolder> {
    private List<Star> starList;
    private Context context;

    public StarAdapter(List<Star> starList, Context context) {
        this.starList = starList;
        this.context = context;
    }

    @NonNull
    @Override
    public StarAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_text,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StarAdapter.ViewHolder holder, int position) {
        holder.textView.setText(starList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return starList == null ? 0 : starList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.rv_text);
        }
    }

    /**
     * 是否是组的头部
     * @param position
     * @return
     */
    public boolean isGroupHead(int position){
        if(position == 0){
            return true;
        }else{
            String currentGroupName = getGroupName(position);
            String preGroupName = getGroupName(position - 1);
            if (currentGroupName.equals(preGroupName)){
                return false;
            }else
                return true;
        }
    }

    /**
     *
     * @param position
     * @return
     */
    public String getGroupName(int position){
        return starList.get(position).getGroupName();
    }
}
